<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>添加优惠劵</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" th:href="@{/plugins/layui/css/layui.css}" media="all" />
	<link rel="stylesheet" th:href="@{/plugins/font-awesome/css/font-awesome.min.css}">
</head>

<body>
	<div style="margin: 15px;">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>添加优惠劵</legend>
		</fieldset>
		<form class="layui-form" enctype="multipart/form-data" class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">名称</label>
				<div class="layui-input-block">
					<input type="text" value="" name="cName" lay-verify="required" autocomplete="off" placeholder="请输入优惠劵名称" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">使用范围</label>
				<div class="layui-input-inline">
					<select name="productType" lay-verify="required">
		          <option value="">选择使用范围</option>
							<option value="0">场地</option>
							<option value="1">布置商品</option>
							<option value="2">达人服务</option>
							<option value="3">包车服务</option>
							<option value="4">餐饮服务</option>
							<option value="4">套餐</option>
		           <option value="6">所有</option>
		        </select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">满多少可用</label>
				<div class="layui-input-block">
					<input type="text" value="" name="amountNeed" lay-verify="required|price" autocomplete="off" placeholder="满多少可用" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">减多少</label>
				<div class="layui-input-block">
					<input type="text" value="" name="favourablePrice" lay-verify="required|price" autocomplete="off" placeholder="减多少" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">发放数量</label>
				<div class="layui-input-block">
					<input type="text" value="" name="cNumber" lay-verify="required|number" autocomplete="off" placeholder="发放数量" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">范围选择</label>
				<div class="layui-input-inline">
					<input class="layui-input" name="startTime" lay-verify="required" placeholder="开始日期" id="LAY_demorange_s">
				</div>
				<div class="layui-input-inline">
					<input class="layui-input" name="endTime" lay-verify="required" placeholder="截止日期" id="LAY_demorange_e">
				</div>
			</div>
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">规则</label>
				<div class="layui-input-block">
					<textarea class="layui-textarea layui-hide" name="rule" id="rule"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<input class="layui-btn" type="button" lay-submit="" lay-filter="add" value="提交">
				</div>
			</div>
		</form>
	</div>

	<script type="text/javascript" src="..//plugins/layui/layui.js"></script>
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/template.js"></script>
	<script type="text/javascript" src="../js/public.js"></script>
	<script>
		layui.use(['form', 'layedit', 'laydate', 'element'], function() {
			var form = layui.form(),
				layer = layui.layer,
				layedit = layui.layedit,
				laydate = layui.laydate;
			var $ = layui.jquery,
				element = layui.element();

			layedit.set({
				uploadImage: {
					url: '/admin//image/simple/upload',
					type: 'post',
					data: 'image',
					success: function(res) {
						res = res.parseJSON();
						console.log(res.result.imgUrl);
					}
				}
			});

			var start = {
				min: laydate.now(),
				max: '2099-06-16 23:59:59',
				istoday: false,
				choose: function(datas) {
					end.min = datas; //开始日选好后，重置结束日的最小日期
					end.start = datas //将结束日的初始值设定为开始日
				}
			};

			var end = {
				min: laydate.now(),
				max: '2099-06-16 23:59:59',
				istoday: false,
				choose: function(datas) {
					start.max = datas; //结束日选好后，重置开始日的最大日期
				}
			};

			document.getElementById('LAY_demorange_s').onclick = function() {
				start.elem = this;
				laydate(start);
			}
			document.getElementById('LAY_demorange_e').onclick = function() {
				end.elem = this
				laydate(end);
			}
			//创建一个编辑器
			var rule = layedit.build('rule');
			//自定义验证规则

			form.verify({
				price: function(value, item) {
					if (isNaN(value)) {
						return "价格格式不正确";
					}
				}
			});

			form.on('submit(add)', function(data) {

				var rules = layedit.getContent(rule);
				
				var url = '/admin/coup/add';
				$.ajax({
					url: url,
					type: "post",
					datatype: "json",
					data: {
						cName:data.field.cName,
						productType:data.field.productType,
						amountNeed:data.field.amountNeed,
						favourablePrice:data.field.favourablePrice,
						cNumber:data.field.cNumber,
						startTime:data.field.startTime,
						endTime:data.field.endTime,
						rule:rules,
					},
					success: function(data) {
						if (data.code == 200) {
							layer.open({
								content: '添加成功去上传图片后就可以在前台显示',
								btn: ['去上传图片', '取消'],
								yes: function(index, layero) {
									//按钮【按钮一】的回调
									location.href = "/admin/img?type=优惠劵&id=" + data.result;
								}
							});
						}
					}
				});
				return false;
			});
		});
	</script>
</body>

</html>
